package com.bluemobi.system.ctl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.bluemobi.framework.base.BaseCtl;
import com.bluemobi.framework.base.Page;
import com.bluemobi.framework.common.Servlets;
import com.bluemobi.framework.constant.SystemConstant;
import com.bluemobi.system.model.SysDic;
import com.bluemobi.system.service.SysDicService;

/**
 * 数据字典Ctl
 *
 * @author wangchen
 */
@Controller
@RequestMapping("system/dic")
public class SysDicCtl extends BaseCtl {

	@Resource
	private SysDicService sysDicService;

	/**
	 * 根据条件查询数据字典列表分页
	 *
	 * @param pageNO
	 *            页数
	 * @param pageSize
	 *            每页条数
	 * @param sort
	 *            排序
	 * @param model
	 *            存储
	 * @param request
	 *            request
	 * @return String
	 */
	@RequiresPermissions("system_dic")
	@RequestMapping(value = "")
	public String toIndex(@RequestParam(value = "pageNO", defaultValue = "0") Integer pageNO,
			@RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "a.create_date DESC") String sort, ModelMap model,
			HttpServletRequest request) {
		try {
			model.put("typeList", sysDicService.getDicTypeList());
			Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
			model.put("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
			model.put("page",
					sysDicService.getListByCondition(searchParams, new Page(pageNO, pageSize, 0, sort, null)));
			SysDic sysDic = new SysDic();
			BeanUtils.populate(sysDic, searchParams);
			model.put("sysDic", sysDic);
		} catch (Exception e) {
			logger.error("根据条件查询数据字典列表分页异常:" + e.getMessage());
			Map<String, Object> map = new HashMap<>();
			map.put("status", SystemConstant.RETURN_STATUS_FAIL);
			map.put("msg", SystemConstant.RETURN_MSG_ERROR);
			model.put("msg", JSON.toJSONString(map));
		}
		return "/system/dic/index";
	}
}
